<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/14
 * Time: 10:42
 */

namespace App\Http\Controllers\HumanResourceManage;


use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Model\SupplierModel;
use App\Http\Model\SupplierOrdersModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class SupplierController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function lists(Request $request)
    {
        $rules = [
            'length' => 'required|integer|in:10,20,50',
            'page' => 'required|integer|min:1',
        ];
        $message = [
            'length.required' => '获取记录条数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.required' => '获取起始记录位置失败',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->only('length','page','search');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $supplierModel = new SupplierModel();
            $lists = $supplierModel->lists($param);
            $countLists = $supplierModel->countLists($param);
            $this->data = $this->tableFormat($lists,$countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 440101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function add(Request $request)
    {
        $rules = [
            'name' => 'required',
            'liaison' => 'required',
            'phone' => 'required',
            'address' => 'required',
            'accountName' => 'required',
            'accountBank' => 'required',
            'accountNumber' => 'required',
            'corporation' => 'required',
        ];
        $message = [
            'name.required' => '请输入供应商名称',
            'liaison.required' => '请输入联系人',
            'phone.required' => '请输入联系人电话',
            'address.required' => '请输入供应商地址',
            'accountName.required' => '请输入开户名称',
            'accountBank.required' => '请输入开户银行',
            'accountNumber.required' => '请输入开户账号',
            'corporation.required' => '请输入法定代表人',
        ];
        $param = $request->only(['name', 'liaison', 'phone', 'address', 'accountName', 'accountBank', 'accountNumber', 'corporation']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $supplierModel = new SupplierModel();
            $info = $supplierModel->checkRepeat([
                'name'=>$param['name'],
                'accountName'=>$param['accountName'],
                'accountNumber'=>$param['accountNumber'],
            ]);
            if (!empty($info)){
                throw new ApiException('公司名称、开户名称或开户账号已存在', 440202);
            }
            $supplierModel->insert($param);
        } else {
            throw new ApiException($validator->errors()->first(), 440201);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 供应商详情
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function info(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取供应商参数失败',
            'id.integer' => '供应商参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $supplierModel = new SupplierModel();
            $this->data = $supplierModel->info($param);
        } else {
            throw new ApiException($validator->errors()->first(), 440301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 编辑供应商信息
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function edit(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'name' => 'required',
            'liaison' => 'required',
            'phone' => 'required',
            'address' => 'required',
            'accountName' => 'required',
            'accountBank' => 'required',
            'accountNumber' => 'required',
            'corporation' => 'required',
        ];
        $message = [
            'id.required' => '获取供应商参数失败',
            'id.integer' => '供应商参数类型不正确',
            'name.required' => '请输入供应商名称',
            'liaison.required' => '请输入联系人',
            'phone.required' => '请输入联系人电话',
            'address.required' => '请输入供应商地址',
            'accountName.required' => '请输入开户名称',
            'accountBank.required' => '请输入开户银行',
            'accountNumber.required' => '请输入开户账号',
            'corporation.required' => '请输入法定代表人',
        ];
        $param = $request->only(['id', 'name', 'liaison', 'phone', 'address', 'accountName', 'accountBank', 'accountNumber', 'corporation']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $supplierModel = new SupplierModel();
            $info = $supplierModel->checkRepeat([
                'name'=>$param['name'],
                'accountName'=>$param['accountName'],
                'accountNumber'=>$param['accountNumber'],
            ],$param['id']);
            if (!empty($info)){
                throw new ApiException('公司名称、开户名称或开户账号已存在', 440402);
            }
            $id = $param['id'];
            unset($param['id']);
            $supplierModel->update(['id'=>$id],$param);
        } else {
            throw new ApiException($validator->errors()->first(), 440401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 删除供应商
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function delete(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取供应商参数失败',
            'id.integer' => '供应商参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $supplierModel = new SupplierModel();
            $supplierOrdersModel = new SupplierOrdersModel();
            $result = $supplierOrdersModel->countLists($param);
            if ($result > 0){
                throw new ApiException('该供应商下已有货单不能被删除', 440502);
            }
            $supplierModel->delete($param);
        } else {
            throw new ApiException($validator->errors()->first(), 440501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 变更供应商使用状态，控制供应商是否在下拉列表中显示
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function editStatus(Request $request){
        $rules = [
            'id' => 'required|integer',
            'status' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取供应商参数失败',
            'id.integer' => '供应商参数类型不正确',
            'status.required' => '获取供应商状态失败',
            'status.integer' => '供应商状态参数类型错误',
        ];
        $param = $request->only(['id', 'status']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $supplierModel = new SupplierModel();
            $supplierModel->update(['id'=>$param['id']],['status'=>$param['status']]);
        } else {
            throw new ApiException($validator->errors()->first(), 440401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @return \Illuminate\Http\Response
     */
    public function selectLists(){
        $supplierModel = new SupplierModel();
        $this->data = $supplierModel->selectLists();
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }
}